from django.core.management.base import BaseCommand
import tsl_backend.users.models as users


class Command(BaseCommand):
    help = "Create Role Permission"

    def add_arguments(self, parser):
        parser.add_argument("--create", type=str)

    
    def handle(self, *args, **options):
        print(options)
        if options["create"]=="all":
            role_objs = users.Role.objects.all()
            flow_objs = users.Flow.objects.all()
            for flow_obj in flow_objs:
                for role_obj in role_objs:
                    role_perm_obj, created = users.RolePermission.objects.get_or_create(role=role_obj, flow=flow_obj)
                    print(f"Create {role_obj} permission for flow {flow_obj}: {created}")

                    if role_obj.name.lower()=="worker":
                        self.set_worker_permission(role_perm_obj)
                    
                    elif role_obj.name.lower()in ["qc", "teamleader", "stoneqc"]:
                        self.set_qc_permission(role_perm_obj)

                    else:
                        self.set_others_permission(role_perm_obj)

            self.stdout.write(
                self.style.SUCCESS('Successfully to delete all orders')
            )

    def set_worker_permission(self, role_perm_obj):
        print("Set worker permission")
        role_perm_obj.menu_massscan_perm = True
        role_perm_obj.menu_orderview_perm = True
        role_perm_obj.menu_printlabel_perm = False
        role_perm_obj.report_worker_storage_perm = True
        role_perm_obj.report_worker_transaction_perm = True
        role_perm_obj.report_worker_handset_perm = True
        role_perm_obj.report_qc_handset_perm = False
        role_perm_obj.report_qc_transaction_perm = False
        role_perm_obj.report_printstatus_perm = False
        role_perm_obj.report_leadtime_perm = False
        role_perm_obj.menu_updatert_perm = False
        role_perm_obj.save()

    
    def set_qc_permission(self, role_perm_obj):
        print("Set QC/leader/stoneQC permission")
        role_perm_obj.menu_massscan_perm = True
        role_perm_obj.menu_orderview_perm = True
        role_perm_obj.menu_printlabel_perm = False
        role_perm_obj.report_worker_storage_perm = False
        role_perm_obj.report_worker_transaction_perm = False
        role_perm_obj.report_worker_handset_perm = False
        role_perm_obj.report_qc_handset_perm = True
        role_perm_obj.report_qc_transaction_perm = True
        role_perm_obj.report_printstatus_perm = True
        role_perm_obj.report_leadtime_perm = False
        role_perm_obj.menu_updatert_perm = False
        role_perm_obj.save()

    
    def set_others_permission(self, role_perm_obj):
        print("Set others permission")
        role_perm_obj.menu_massscan_perm = False
        role_perm_obj.menu_orderview_perm = True
        role_perm_obj.menu_printlabel_perm = True
        role_perm_obj.report_worker_storage_perm = False
        role_perm_obj.report_worker_transaction_perm = False
        role_perm_obj.report_worker_handset_perm = False
        role_perm_obj.report_qc_handset_perm = True
        role_perm_obj.report_qc_transaction_perm = True
        role_perm_obj.report_printstatus_perm = True
        role_perm_obj.report_leadtime_perm = True
        role_perm_obj.menu_updatert_perm = True
        role_perm_obj.save()


